
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;

public class LoadScreen extends GameCanvas {

    private Graphics g;
    private String logo = "/resource/logo.PNG";
    private String text1 = "Loading";
    private Image imageLogo;
    private Point root = new Point(30, 50);
    private Font fontNormal;
    private Font fontBold;
    //thanh loading
    private int colorBorder = 0x000000;
    private int colorLoading = 0xcc3301;
    private int loadingLength = 150;
    private int loadingHeight = 5;
    private int loadingCurrent = 0;
    private int h = 10;

    public LoadScreen() {

        super(false);
        this.g = this.getGraphics();
        this.fontNormal = g.getFont();
        this.fontBold = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        setFullScreenMode(true);
        try {
            imageLogo = Image.createImage(logo);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        this.root = new Point(getWidth() / 2 - imageLogo.getWidth() / 2, root.getY());

    }

    public void start() {
        clearScreen();
        draw();
        draw_next_loading();
        //clearLoading();
    }

    public void draw() {
        g.setColor(0x000000);
        g.drawString(text1, getWidth() / 2 - fontBold.stringWidth(text1) / 2, root.getY() + imageLogo.getHeight() + 40, Graphics.TOP | Graphics.LEFT);
        g.setFont(fontNormal);
        g.drawImage(imageLogo, root.getX(), root.getY(), Graphics.TOP | Graphics.LEFT);
        //draw loading
        g.setColor(colorBorder);
        g.drawRect(getWidth() / 2 - loadingLength / 2, root.getY() + imageLogo.getHeight() + 30, loadingLength, loadingHeight);
        g.setColor(colorLoading);
        g.fillRect(getWidth() / 2 - loadingLength / 2, root.getY() + imageLogo.getHeight() + 30, loadingCurrent, loadingHeight);
        g.setColor(colorBorder);
        this.flushGraphics();
    }

    private void draw_next_loading() {
        clearLoading();
        //timer.schedule(task, interval,interval);
        for (int i = 0; i < loadingLength / h; i++) {
            if (loadingCurrent < loadingLength) {
                loadingCurrent += h;
                //draw loading
                g.setColor(colorBorder);
                g.drawRect(getWidth() / 2 - loadingLength / 2, root.getY() + imageLogo.getHeight() + 30, loadingLength, loadingHeight);
                g.setColor(colorLoading);
                g.fillRect(getWidth() / 2 - loadingLength / 2, root.getY() + imageLogo.getHeight() + 30, loadingCurrent, loadingHeight);
                g.setColor(colorBorder);
                this.flushGraphics();
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                }
            } else {
                loadingCurrent = 0;
                break;
            }
        }
    }

    private void clearLoading() {
        g.setColor(0xffffff);
        g.fillRect(getWidth() / 2 - loadingLength / 2, root.getY() + imageLogo.getHeight() + 30, loadingLength, loadingHeight);
        this.flushGraphics();
    }

    public void clearScreen() {
        g.setColor(0xffffff); // white
        g.fillRect(0, 0, getWidth(), getHeight());
        this.flushGraphics();
    }
}
